<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\CategoryModel;

class Category extends Common
{
	/**
	*	商品分类列表
	*/
	public function index()
	{
		$data = $this->request->param();
		$CategoryModel = new CategoryModel();
		$list = $CategoryModel->categoryIndex($data);
		// $page = $list->render();
		// $this->assign('page',$page);
		$this->assign('list',$list);
		$this->assign('name',isset($data['name']) ? $data['name'] : '');
		return $this->fetch();
	}

	/**
	*	添加顶级分类
	*/
	public function add()
	{
		return $this->fetch();
	}

	/**
	*	添加顶级分类提交
	*/
	public function addPost()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$CategoryModel = new CategoryModel();
			$list = $CategoryModel->addCategory($data);
			if($list){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}
	}

	/**
	*	修改顶级分类
	*/
	public function edit()
	{
		$data = $this->request->param();
		if($data){
			$CategoryModel = new CategoryModel();
			$list = $CategoryModel->getOne($data['id']);
			if($list){
				$this->assign('list',$list);
			}
		}

		return $this->fetch();
	}

	/**
	*	修改顶级分类提交
	*/
	public function editPost()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$CategoryModel = new CategoryModel();
			$list = $CategoryModel->editCategory($data['id'],$data);
			$this->success('修改成功',url('/admin/Category/index'));
		}
	}

	/**
	*	添加顶级分类的子类
	*/
	public function addChild()
	{
		$data = $this->request->param();

		if($data){
			$this->assign('list',$data);
		}
		return $this->fetch();
	}


	/**
	*	添加顶级分类的子类提交
	*/
	public function addChildPost()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$CategoryModel = new CategoryModel();
			$list = $CategoryModel->addChildCategory($data['pid'],$data['path'],$data);
			if($list){
				$this->success('添加成功',url('/admin/Category/index'));
			}else{
				$this->error('添加失败');
			}
		}
	}

	/**
	*	修改顶级分类提交
	*/
	public function editChildPost()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$CategoryModel = new CategoryModel();
			$list = $CategoryModel->editCategoryChild($data['id'],$data);
			$this->success('修改成功',url('/admin/Category/index'));
		}
	}

	/**
	*	修改子类
	*/
	public function editChild()
	{
		$data = $this->request->param();
		if($data){
			$CategoryModel = new CategoryModel();
			$list = $CategoryModel->getChildOne($data['id']);
			if($list){
				$this->assign('list',$list);
			}
		}

		return $this->fetch();
	}

	/**
	*	删除分类
	*/
	public function delete()
	{
		$data = $this->request->param();
		$CategoryModel = new CategoryModel();
		$list = $CategoryModel->deleteCategory($data['id']);
		if($list == 2){
			$this->error('该分类下有子类，不能删除！',url('/admin/Category/index'));
		}elseif($list == 3){
			$this->error('该分类下有商品，不能删除！',url('/admin/Category/index'));
		}elseif($list){
			$this->success('删除成功',url('/admin/Category/index'));
		}else{
			$this->error('删除失败',url('/admin/Category/index'));
		}
	}
}